package edu.zqu.collegeField.project.controller;


import cn.hutool.core.util.StrUtil;
import edu.zqu.collegeField.project.po.FieldType;
import edu.zqu.collegeField.project.service.FieldTypeService;
import edu.zqu.collegeField.project.vo.FieldTypeVo;
import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

/**
 * <p>
 * 场地类型 前端控制器
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@RestController
@RequestMapping("/api")
@Api(tags = "场地类型")
public class FieldTypeController {
    @Autowired
    private FieldTypeService fieldTypeService;

    @ApiOperation(value = "获取场地类型列表")
    @GetMapping(value = "/fieldTypes")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "类型名称", paramType = "query", required = false)
    })
    public ZQUResult get(BigInteger collegeId, String name) {
        List<FieldTypeVo> fieldTypeVoList = fieldTypeService.get(collegeId, StrUtil.trimToNull(name));
        if (StringUtils.isNotEmpty(fieldTypeVoList)) {
            return new ZQUResult(HttpStatus.SUCCESS, "请求成功", fieldTypeVoList);
        } else return new ZQUResult(HttpStatus.NO_CONTENT, "请求失败", null);
    }

    @ApiOperation(value = "获取场地类型信息", response = FieldTypeVo.class)
    @GetMapping(value = "/fieldType/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场地类型id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        FieldTypeVo fieldTypeVo = fieldTypeService.getById(id);
        if (StringUtils.isNotNull(fieldTypeVo)) {
            return new ZQUResult(HttpStatus.SUCCESS, "请求成功", fieldTypeVo);
        } else return new ZQUResult(HttpStatus.NO_CONTENT, "请求失败", null);
    }


    @ApiOperation(value = "获取场地类型名称", response = FieldTypeVo.class)
    @GetMapping(value = "/fieldTypeName/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场地类型id", paramType = "path", required = true)
    })
    public ZQUResult getNameById(@PathVariable BigInteger id) {
        String fieldTypeName = fieldTypeService.getNameById(id);
        if (StringUtils.isNotNull(fieldTypeName)) {
            return new ZQUResult(HttpStatus.SUCCESS, "请求成功", fieldTypeName);
        } else return new ZQUResult(HttpStatus.NO_CONTENT, "请求失败", null);
    }


    @ApiOperation(value = "添加")
    @PostMapping(value = "/fieldType")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "FieldType", name = "FieldType", value = "场地类型", paramType = "query", required = true)
    })
    public ZQUResult add(@RequestBody FieldType fieldType) {
        int rs = fieldTypeService.add(fieldType);
        if (rs >= 1) {
            return new ZQUResult(HttpStatus.SUCCESS, "添加成功");
        }
        return new ZQUResult(HttpStatus.ERROR, "添加失败", null);
    }

    @ApiOperation(value = "更新")
    @PutMapping(value = "/fieldType")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "FieldType", name = "FieldType", value = "场地类型", paramType = "query", required = true)
    })
    public ZQUResult update(@RequestBody FieldType fieldType) {
        int rs = fieldTypeService.update(fieldType);
        if (rs >= 1) {
            return new ZQUResult(HttpStatus.SUCCESS, "修改成功", null);
        }
        return new ZQUResult(HttpStatus.ERROR, "修改失败", null);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/fieldType/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类型id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable Integer id,
                            BigInteger byWho) {
        int rs = fieldTypeService.delete(id, byWho);
        if (rs >= 1) {
            return new ZQUResult(HttpStatus.SUCCESS, "删除成功", null);
        }
        return new ZQUResult(HttpStatus.ERROR, "删除失败", null);
    }
}

